#include "stdafx.h"
#include "SimpleModel.h"
#include <assimp\cimport.h>
#include <assimp\Importer.hpp>
#include <assimp\scene.h>
#include <assimp\postprocess.h>
#include "ResourceManager.h"

SimpleModel::SimpleModel()
{
	mNumMesh = 0;
	meshData = 0;
}


SimpleModel::~SimpleModel()
{
	Delete_Array(meshData);
	Delete(meshData);
}


bool SimpleModel::LoadModelFromFile(char* path2Model)
{
	Assimp::Importer importer;

	const aiScene* scene = importer.ReadFile(path2Model,
		aiProcess_GenSmoothNormals |
		aiProcess_CalcTangentSpace |
		aiProcess_Triangulate |
		aiProcess_JoinIdenticalVertices |
		aiProcess_SortByPType
		);

	if (!scene)
	{
		esLogMessage("Error loading model (assim) ------- path:\n");
		esLogMessage(path2Model);
		esLogMessage("Error Code:\n");
		esLogMessage(importer.GetErrorString());
		return false;
	}

	//Load Mesh
	if (scene->HasMeshes())
	{
		mNumMesh = scene->mNumMeshes;
		meshData = new SimpleMesh*[mNumMesh];
		for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
		{
			meshData[i] = new SimpleMesh();
			aiMesh *mesh = scene->mMeshes[i];
			int iMeshFaces = mesh->mNumFaces;
			meshData[i]->vertexData = new Vertex[mesh->mNumVertices];
			meshData[i]->numVertex = mesh->mNumVertices;
			//Get Mesh vertices data
			for (int j = 0; j < iMeshFaces; j++)
			{
				const aiFace& face = mesh->mFaces[j];
				for (int k = 0; k < 3; k++)
				{
					aiVector3D pos = mesh->mVertices[face.mIndices[k]];
					aiVector3D uv = mesh->mTextureCoords[0][face.mIndices[k]];
					aiVector3D normal = mesh->HasNormals() ? mesh->mNormals[face.mIndices[k]] : aiVector3D(1.0f, 1.0f, 1.0f);
					meshData[i]->vertexData[i].position = Vector3(pos.x, pos.y, pos.z);
					meshData[i]->vertexData[i].texcoord = Vector2(uv.x, uv.y);
					meshData[i]->vertexData[i].normal = Vector3(normal.x, normal.y, normal.z);
				}
			}
			//Get Mesh indices
			meshData[i]->indicesData = new GLushort[mesh->mNumFaces * 3];
			for (int t = 0; t < mesh->mNumFaces; ++t)
			{
				aiFace* face = &mesh->mFaces[t];
				if (face->mNumIndices != 3)
				{
					esLogMessage("Not triangle form\n");
					continue;
				}
				meshData[i]->indicesData[meshData[i]->numIndex++] = face->mIndices[0];
				meshData[i]->indicesData[meshData[i]->numIndex++] = face->mIndices[1];
				meshData[i]->indicesData[meshData[i]->numIndex++] = face->mIndices[2];
			}
		}
	}

	//Load and map texture

	if (scene->HasMaterials())
	{
		int iNumMaterials = scene->mNumMaterials;

		for (int i = 0; i < iNumMaterials; i++)
		{
			const aiMaterial* material = scene->mMaterials[i];
			int a = 5;
			int texIndex = 0;
			aiString path;  // filename

			if (material->GetTexture(aiTextureType_DIFFUSE, texIndex, &path) == AI_SUCCESS)
			{
				string sTextureName = path.data;
				string sFullPath = "..\\Resources\\Texture\\" + sTextureName;
				int iTexFound = -1;
				Texture* temp = new Texture();
				const char* ch = sFullPath.c_str();
				char path[1024];
				strcpy_s(path, 1024, ch);
				if (temp->Load2DTexture(path) != NULL)
				{
					ResourceManager::GetInstance()->AddTexture(temp);
					meshData[i]->textureMapId = ResourceManager::GetInstance()->countTexture-1;
				}
			}
		}
	}
	return true;
}

void Render()
{

}